package com.nanfang51g3.ckz.ui.base;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SystemBarTintManager;
import com.nanfang51g3.ckz.widget.LoadingDialog;
import com.nanfang51g3.ckz.widget.view.SwipeBackLayout;
import com.umeng.analytics.MobclickAgent;


/**
 * Created by Administrator on 2016/4/19.
 */
public abstract class Base2Activity<T extends IPresenter> extends AppCompatActivity {
    private LoadingDialog dialog;
    private SwipeBackLayout swipeBackLayout;
    private ImageView ivShadow;

    protected void initViewHeader() {
        Toolbar id_toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        if (id_toolbar != null) {
            id_toolbar.setTitle("");
            setSupportActionBar(id_toolbar);

            id_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickLeft(v);
                }
            });
            //标题栏信息
            TextView index_tv_title = (TextView) findViewById(R.id.index_tv_title);
            index_tv_title.setText(getResources().getString(showTitle()));
            //是否显示左边图标
            if (isShowLeftIcon()) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                id_toolbar.setNavigationIcon(setLeftIcon());
            } else {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            }
            //是否显示右边图标
            if (isShowRightIcon()) {
                ImageView index_iv_right = (ImageView) findViewById(R.id.index_iv_right);
                index_iv_right.setVisibility(View.VISIBLE);
                index_iv_right.setBackgroundResource(setRightIcon());
                index_iv_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickRight(v);

                    }
                });
            }
        }
    }

    private View getContainer() {
        RelativeLayout container = new RelativeLayout(this);
        swipeBackLayout = new SwipeBackLayout(this);
        swipeBackLayout.setDragEdge(SwipeBackLayout.DragEdge.LEFT);
        ivShadow = new ImageView(this);
        ivShadow.setBackgroundColor(getResources().getColor(R.color.theme_black_7f));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        container.addView(ivShadow, params);
        container.addView(swipeBackLayout);
        swipeBackLayout.setOnSwipeBackListener(new SwipeBackLayout.SwipeBackListener() {
            @Override
            public void onViewPositionChanged(float fa, float fs) {
                ivShadow.setAlpha(1 - fs);
            }
        });
        return container;
    }

    @Override
    public void setContentView(int layoutResID) {
        if (layoutResID == R.layout.home) {
            super.setContentView(layoutResID);
        } else {
            super.setContentView(getContainer());
            View view = LayoutInflater.from(this).inflate(layoutResID, null);
            view.setBackgroundColor(getResources().getColor(R.color.window_background));
            swipeBackLayout.addView(view);
        }
    }

    public void showPro(int title) {
        dialog = new LoadingDialog(this, R.layout.view_tips_loading, title);
        dialog.setCancelable(true);//返回取消
        dialog.setCanceledOnTouchOutside(true);//触摸取消
        dialog.show();
        KLog.i("TAG", dialog == null ? "showPro==null" : "showPro!=null");
    }


    public void dismissPro() {
        KLog.i("TAG", dialog == null ? "dialog==null" : "dialog!=null");
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    protected T mPresenter;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KLog.i("Evd", "onDestroy");
        if (mPresenter != null) mPresenter.detachView();
        AppManager.getAppManager().finishActivity(this);
    }

    public abstract T getPresenter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        onIntent();
        this.setContentView(this.getContentView());
        initViewHeader();
        mPresenter = getPresenter();

        //判断SDK版本是否大于或等于19
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.theme_transparent);//通知栏所需颜色
        tintManager.setStatusBarTintResource(R.color.status_bar_bg);//通知栏所需颜色
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    public void onIntent() {

    }

    /**
     * 加载布局文件
     *
     * @return
     */
    public abstract int getContentView();

    /**
     * 是否左边图标按钮
     *
     * @return
     */
    public abstract boolean isShowLeftIcon();

    /**
     * 是否右边图标按钮
     *
     * @return
     */
    public abstract boolean isShowRightIcon();

    /**
     * 更新右边图标按钮
     *
     * @return
     */
    public abstract int setLeftIcon();

    /**
     * 更新右边图标按钮
     *
     * @return
     */
    public abstract int setRightIcon();

    /**
     * 显示标题栏
     *
     * @return
     */
    public abstract int showTitle();

    /**
     * 左边点击事件
     *
     * @param v
     */
    public abstract void onClickLeft(View v);

    /**
     * 右边点击事件
     *
     * @param v
     */
    public abstract void onClickRight(View v);

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
